package com.ruoyi.oar.controller.pc;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Response;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oar.domain.dto.OarUserDTO;
import com.ruoyi.oar.domain.query.OarUserQuery;
import com.ruoyi.oar.service.OarUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * 从业人员Controller
 * 
 * @author ruoyi
 * @date 2022-02-17
 */
@Api(tags = "PC端从业人员")
@RestController
@RequestMapping("/pc/oar-users")
public class PcUserController extends BaseController
{
    @Autowired
    private OarUserService oarUserService;

    @ApiOperation("查询从业人员列表")
    @GetMapping
    public TableDataInfo<OarUserDTO> list(OarUserQuery oarUserQuery){
        logger.debug("查询从业人员列表,查询条件oarUserQuery={}",JSONObject.toJSONString(oarUserQuery));
        startPage();
        return oarUserService.selOarUserAndHouseVo(oarUserQuery);
    }

    /**
     * 导出从业人员列表
     */
    @ApiOperation("导出从业人员列表")
    @Log(title = "从业人员", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(HttpServletResponse response, OarUserQuery oarUserQuery){
        logger.debug("查询从业人员列表,导出条件oarUserQuery={}",JSONObject.toJSONString(oarUserQuery));
        List<OarUserDTO> list = oarUserService.selectOarUserList(oarUserQuery);
        ExcelUtil<OarUserDTO> util = new ExcelUtil<OarUserDTO>(OarUserDTO.class);
        util.exportExcel(response,list, "从业人员数据");
    }

    /**
     * 获取从业人员详细信息
     */
    @ApiOperation("获取从业人员详细信息")
    @GetMapping(value = "/{id}")
    public Response<OarUserDTO> getInfo(@PathVariable("id") @ApiParam("从业人员ID") String id) {
        logger.debug("获取从业人员详细信息,id={}",id);
        checkId(id);
        return Response.success(oarUserService.selectOarUserById(id).orElseThrow(()->new ServiceException("无效的ID")));
    }

    /**
     * 新增从业人员
     */
    @ApiOperation("新增从业人员")
    @Log(title = "从业人员", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@RequestBody OarUserDTO oarUserDTO) {
        logger.debug("新增从业人员,oarUserDTO={}",JSONObject.toJSONString(oarUserDTO));
        return toAjax(oarUserService.insertOarUser(oarUserDTO));
    }

    /**
     * 修改从业人员
     */
    @ApiOperation("修改从业人员")
    @Log(title = "从业人员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OarUserDTO oarUserDTO) {
        logger.debug("修改从业人员,oarUserDTO={}",JSONObject.toJSONString(oarUserDTO));
        checkId(oarUserDTO.getId());
        return toAjax(oarUserService.updateOarUser(oarUserDTO));
    }

    /**
     * 删除从业人员
     */
    @ApiOperation("删除从业人员")
    @Log(title = "从业人员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        logger.debug("删除从业人员,ids={}",JSONObject.toJSONString(ids));
        Arrays.asList(ids).forEach(this::checkId);
        return toAjax(oarUserService.deleteOarUserByIds(ids));
    }

    /**
     * 获根据从业人员id查询从业人员及房屋信息列表
     */
    @ApiOperation("根据从业人员id查询从业人员及房屋信息列表")
    @GetMapping(value = "/selUserAndHouseById")
    public Response<OarUserDTO> selUserAndHouseById(String id) {
        logger.debug("获取从业人员详细信息,id={}",id);
        checkId(id);
        return Response.success(oarUserService.selUserAndHouseById(id).orElseThrow(()->new ServiceException("无效的ID")));
    }

    /**
     * 校验
     * @param id
     */
    private void checkId(String id) {
        Optional.ofNullable(id).orElseThrow(() -> new ServiceException("3"));
        oarUserService.selectOarUserById(id).orElseThrow(() -> new ServiceException("无效的ID"));
    }
}
